package com.huluobo.a2112mvvmdouyindemo.mine;


import android.content.Intent;
import android.util.Log;
import android.view.View;

import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.viewpager2.widget.ViewPager2;


import com.blankj.utilcode.util.SPUtils;
import com.flyco.tablayout.listener.CustomTabEntity;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.huluobo.a2112mvvmdouyindemo.R;
import com.huluobo.a2112mvvmdouyindemo.adapter.CommonPagerAdapter;
import com.huluobo.a2112mvvmdouyindemo.collect.CollectFragment;
import com.huluobo.a2112mvvmdouyindemo.databinding.FragmentMineBinding;
import com.huluobo.a2112mvvmdouyindemo.home.TabEntity;
import com.huluobo.a2112mvvmdouyindemo.like.LikeFragment;
import com.huluobo.a2112mvvmdouyindemo.setting.SettingActivity;
import com.huluobo.a2112mvvmdouyindemo.utils.ImageLoader;
import com.huluobo.mvvm_base.bean.Money;
import com.huluobo.mvvm_base.bean.UserBeanResult;
import com.huluobo.mvvm_base.bean.VideoBeanResult;
import com.huluobo.mvvm_base.db.AppDBUtils;
import com.huluobo.mvvm_base.net.Const;
import com.huluobo.mvvm_base.view.BaseFragment;
import com.huluobo.mvvm_base.view.BaseMVVMFragment;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class MineFragment extends BaseMVVMFragment<FragmentMineBinding, MineViewModel> {

    private ArrayList<CustomTabEntity> tabEntities = new ArrayList<>();

    private CommonPagerAdapter commonPagerAdapter;

    private List<Fragment> fragments = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initData() {
        viewModel.success.observe(this, userBean -> {
            ImageLoader.getInstance().loadCircleImage(requireContext(), userBean.getIcon(), dataBinding.mineUserIconIv);
            dataBinding.mineNicknameTv.setText(userBean.getNickname());
        });
    }

    @Override
    protected void initView() {
        viewModel.getUserInfo(SPUtils.getInstance().getString(Const.SP_USERNAME));

        tabEntities.clear();
        tabEntities.add(new TabEntity("收藏", 0, 0));
        tabEntities.add(new TabEntity("点赞", 0, 0));
        dataBinding.mineTabLayout.setTabData(tabEntities);

        fragments.clear();
        fragments.add(new CollectFragment());
        fragments.add(new LikeFragment());

        commonPagerAdapter = new CommonPagerAdapter(requireActivity(), fragments);
        dataBinding.mineVp2.setAdapter(commonPagerAdapter);

        dataBinding.mineTabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
                dataBinding.mineVp2.setCurrentItem(position);
            }

            @Override
            public void onTabReselect(int position) {

            }
        });

        dataBinding.mineVp2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
                dataBinding.mineTabLayout.setCurrentTab(position);
            }
        });

        dataBinding.mineSettingIv.setOnClickListener(v -> {
            Intent intent =new Intent(requireContext(), SettingActivity.class);
            startActivity(intent);
        });
    }

    @Override
    public void onResume() {
        super.onResume();
        viewModel.getUserInfo(SPUtils.getInstance().getString(Const.SP_USERNAME));
    }

    @Override
    protected Class<MineViewModel> getViewModel() {
        return MineViewModel.class;
    }
}